import random

# 定义玩家类
class Player:
    def __init__(self):
        self.money = 1000
        self.ship_health = 100
        self.inventory = {}
        self.current_island = None

    def buy_goods(self, goods, price):
        if self.money >= price:
            self.money -= price
            if goods in self.inventory:
                self.inventory[goods] += 1
            else:
                self.inventory[goods] = 1
            print(f"你购买了 {goods}，花费 {price} 金币。")
        else:
            print("你的金币不足，无法购买。")

    def sell_goods(self, goods, price):
        if goods in self.inventory and self.inventory[goods] > 0:
            self.inventory[goods] -= 1
            self.money += price
            print(f"你出售了 {goods}，获得 {price} 金币。")
        else:
            print("你没有该货物可出售。")

    def sail_to_island(self, island):
        # 模拟航行过程中的随机事件
        if random.random() < 0.2:
            pirate_attack(self)
        else:
            self.current_island = island
            print(f"你成功抵达了 {island.name}。")
            island.show_market()


# 定义岛屿类
class Island:
    def __init__(self, name, market):
        self.name = name
        self.market = market

    def show_market(self):
        print(f"{self.name} 的市场：")
        for goods, price in self.market.items():
            print(f"{goods}: {price} 金币")


# 定义海盗攻击函数
def pirate_attack(player):
    print("你遭遇了海盗袭击！")
    damage = random.randint(10, 30)
    player.ship_health -= damage
    if player.ship_health <= 0:
        print("你的船被海盗摧毁，游戏结束。")
        exit()
    else:
        print(f"海盗对你的船造成了 {damage} 点伤害，你的船当前生命值为 {player.ship_health}。")


# 初始化岛屿
island1 = Island("黄金岛", {"香料": 200, "丝绸": 300})
island2 = Island("珍珠岛", {"珍珠": 400, "珊瑚": 250})
islands = [island1, island2]

# 初始化玩家
player = Player()

# 游戏主循环
while True:
    print(f"\n你的金币：{player.money}，船的生命值：{player.ship_health}")
    if player.current_island:
        print(f"你当前位于 {player.current_island.name}。")
        action = input("请选择操作：1. 购买货物 2. 出售货物 3. 前往其他岛屿 4. 退出游戏：")
        if action == "1":
            goods = input("请输入要购买的货物名称：")
            if goods in player.current_island.market:
                price = player.current_island.market[goods]
                player.buy_goods(goods, price)
            else:
                print("该岛屿没有此货物出售。")
        elif action == "2":
            goods = input("请输入要出售的货物名称：")
            if goods in player.current_island.market:
                price = player.current_island.market[goods]
                player.sell_goods(goods, price)
            else:
                print("该岛屿不收购此货物。")
        elif action == "3":
            print("可前往的岛屿：")
            for i, island in enumerate(islands):
                if island != player.current_island:
                    print(f"{i + 1}. {island.name}")
            choice = int(input("请选择要前往的岛屿编号：")) - 1
            player.sail_to_island(islands[choice])
        elif action == "4":
            print("感谢游玩，再见！")
            break
        else:
            print("无效的选择，请重新输入。")
    else:
        print("你在海上漂泊，选择一个岛屿前往。")
        for i, island in enumerate(islands):
            print(f"{i + 1}. {island.name}")
        choice = int(input("请选择要前往的岛屿编号：")) - 1
        player.sail_to_island(islands[choice])